﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ItemPicker
{
    [TemplatePart(Name = "PART_PredefinedItems", Type = typeof(ListBox))]    
    [TemplateVisualState(Name = "Thumbnailed", GroupName = "CommonStates")]
    [TemplateVisualState(Name = "Opened", GroupName = "CommonStates")]
    public class ItemPickerControl : Control
    {
        #region Fields

        private ListBox _predefinedItems = null;        

        #endregion Fields
        #region Contructors

        public ItemPickerControl()
            : base()
        {
            DefaultStyleKey = typeof(ItemPickerControl);

            Initialize();
        }

        private void Initialize()
        {
            this.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseLeftButtonDown);
            this.MouseEnter += new MouseEventHandler(OnMouseEnter);
            this.MouseLeave += new MouseEventHandler(OnMouseLeave);
        }

        #endregion Contructors
        #region Events

        public event EventHandler<StringEventArgs> ItemSelected;
        protected void OnItemSelected(StringEventArgs e)
        {
            if (ItemSelected != null)
            {
                ItemSelected(this, e);
            }
        }

        #endregion Events
        #region Methods

        public override void OnApplyTemplate()
        {            
            this._predefinedItems = this.GetTemplateChild("PART_PredefinedItems") as ListBox;
            if (_predefinedItems == null)
            {
                throw new NullReferenceException("Could not find templated PART: PART_PredefinedItems");
            }
            _predefinedItems.SelectionChanged += new SelectionChangedEventHandler(OnPredefinedItemsSelectionChanged);            

            base.OnApplyTemplate();
        }

        private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Show();
        }

        private void Show()
        {
            VisualStateManager.GoToState(this, "Opened", true);
            _predefinedItems.SelectedIndex = -1;
            _predefinedItems.SelectedItem = null;
        }

        private void Hide()
        {            
            VisualStateManager.GoToState(this, "Thumbnailed", true);            
        }        

        private void OnMouseEnter(object sender, MouseEventArgs e)
        {
            Show();
        }

        private void OnMouseLeave(object sender, MouseEventArgs e)
        {
            Hide();
        }

        private void OnPredefinedItemsSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_predefinedItems.SelectedItem != null)
            {
                string value = (string)_predefinedItems.SelectedItem;
                StringEventArgs eventArgs = new StringEventArgs(value);
                OnItemSelected(eventArgs);
                Hide();
            }
        }        

        #endregion Methods
    }
}
